package com.zmn.plat.business.impl.product.productgroup;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.business.interfaces.product.productgroup.ProductGroupBService;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dictionary.ProductDict;
import com.zmn.plat.common.dto.category.CategorySimpleProductGroupDRO;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.product.productgroup.ProductGroup;
import com.zmn.plat.model.entity.product.productgroup.ProductGroupQuery;
import com.zmn.plat.model.entity.servcategory.ServAssCategoryQuery;
import com.zmn.plat.model.vo.product.productgroup.ProductGroupVO;
import com.zmn.plat.model.vo.servcategory.ServAssCategoryVO;
import com.zmn.plat.services.interfaces.product.productgroup.ProductGroupService;
import com.zmn.plat.services.interfaces.servcategory.ServAssCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 描述: 产品组基础接口类
 *
 * @author tanghongjun
 * @since 2020-08-19 11:18:36
 **/
@Service
@Slf4j
public class ProductGroupBServiceImpl implements ProductGroupBService {
    @Resource
    ProductGroupService productGroupService;
    @Resource
    ServAssCategoryService servAssCategoryService;

    @Override
    public List<VtDTO> listVtByBizType(Integer bizType, Integer status) {
        if (NumberUtil.isNullOrZero(bizType)) {
            return Collections.emptyList();
        }

        ProductGroupQuery query = new ProductGroupQuery();
        query.setBizType(bizType);
        query.setStatus(GlobalConsts.YES);
        List<ProductGroupVO> items = productGroupService.listPageByQuery(query);

        if (CollectionUtil.isNullOrEmpty(items)) {
            return Collections.emptyList();
        }

        List<VtDTO> dtos = items.stream().map(productGroup -> new VtDTO(productGroup.getGroupId(), productGroup.getName())).distinct().collect(Collectors.toList());

        return dtos;
    }

    @Override
    public CategorySimpleProductGroupDRO findByCategIdAndBizType(Integer servCategId, Integer categTwoId, Integer bizType) {
        ServAssCategoryQuery query = new ServAssCategoryQuery();
        query.setServCategId(servCategId);
        query.setCategId(categTwoId);
        List<ServAssCategoryVO> servAssCategoryVOS = servAssCategoryService.listByQuery(query);

        if (CollectionUtils.isEmpty(servAssCategoryVOS)) {
            log.error("servAssCategoryVOS is null,errorCode:", PlatErrorCodeDict.ERROR_CODE_SERCATEGORY_RELATIONSHIP_NOT_EXISTS);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_SERCATEGORY_RELATIONSHIP_NOT_EXISTS);
        }

        ServAssCategoryVO servAssCategoryVO = servAssCategoryVOS.get(0);
        Integer groupId = null;
        if (Objects.equals(ProductConsts.BIZ_TYPE_C, bizType)) {
            groupId = servAssCategoryVO.getProductGroupcId();
        } else if (Objects.equals(ProductConsts.BIZ_TYPE_F, bizType)) {
            groupId = servAssCategoryVO.getProductGroupfId();
        } else if (Objects.equals(ProductConsts.BIZ_TYPE_B, bizType)) {
            groupId = servAssCategoryVO.getProductGroupbId();
        }

        if (NumberUtil.isNullOrZero(groupId)) {
            log.error("servCategId：{}，categTwoId：{}，bizType：{}该服务品类下未配置产品组", servCategId, categTwoId, bizType);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_SERCATEGORY_DOESNOT_HAVE_PRODUCT_GROUP);
        }

        ProductGroup productGroup = Optional.ofNullable(productGroupService.findById(groupId)).orElse(new ProductGroup());
        CategorySimpleProductGroupDRO result = new CategorySimpleProductGroupDRO();
        result.setGroupId(groupId);
        result.setGroupCode(productGroup.getGroupCode());
        result.setName(productGroup.getName());
        result.setBizLine(productGroup.getBizLine());

        return result;
    }

    @Override
    public CategorySimpleProductGroupDRO findByShowCategIdAndBizType(Integer servCategId, Integer showCategTwoId, Integer bizType) {
        VtDTO vtDTO = servAssCategoryService.findProductGroupVtByShowCategId(servCategId, showCategTwoId, bizType);

        if (Objects.isNull(vtDTO) || NumberUtil.isNullOrZero(vtDTO.getValue())) {
            log.error("servCategId：{}，showCategTwoId：{}，bizType：{}该服务品类下未配置产品组", servCategId, showCategTwoId, bizType);
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_SERCATEGORY_DOESNOT_HAVE_PRODUCT_GROUP);
        }

        Integer groupId = vtDTO.getValue();
        ProductGroup productGroup = Optional.ofNullable(productGroupService.findById(groupId)).orElse(new ProductGroup());
        CategorySimpleProductGroupDRO result = new CategorySimpleProductGroupDRO();
        result.setGroupId(groupId);
        result.setGroupCode(productGroup.getGroupCode());
        result.setName(productGroup.getName());
        result.setBizLine(productGroup.getBizLine());

        return result;
    }

    /**
     * 描述: 根据条件导出后台产品组报表
     *
     * @param query 查询条件
     * @return java.util.ArrayList<java.util.List>
     * @author wangjie
     * @updater wangjie 2019-11-07 导出添加银联支付状态列
     * @since 14:49 2019/2/19
     **/
    @Override
    public ArrayList<List> outputProductExcel(ProductGroupQuery query) {

        List<ProductGroupVO> productGroupList = productGroupService.listPageByQuery(query);
        ArrayList<List> arrayList = Lists.newArrayListWithExpectedSize(productGroupList.size() + 1);
        List<String> column = Arrays.asList(ProductConsts.ZMN_PRODUCT_GROUP_EXCEL_TITLE);
        arrayList.add(column);

        if (!productGroupList.isEmpty()) {
            productGroupList.forEach(productGroup -> {
                List<String> item = new ArrayList<>();
                item.add(productGroup.getSort().toString());
                item.add(productGroup.getGroupId().toString());
                item.add(productGroup.getName());
                item.add(Objects.isNull(productGroup.getBizType()) ? "-" : ProductDict.getBizTypeCNENName(productGroup.getBizType()));
                item.add(productGroup.getBizLineName());
                item.add(Objects.isNull(productGroup.getStatus()) ? "-" : GlobalDict.getStatus(productGroup.getStatus()));
                item.add(productGroup.getUpdater());
                item.add(Objects.isNull(productGroup.getUpdateTime()) ? "-" : DateUtil.toString(productGroup.getUpdateTime()));

                arrayList.add(item);
            });
        }
        return arrayList;
    }
}
